//
//  WRRootTabBarViewController.m
//  WRReaderCenter
//
//  Created by founder on 15-4-2.
//  Copyright (c) 2015年 com.founder. All rights reserved.
//

#import "RootTabBarViewController.h"
#import "HearderVC.h"
#import "ShopingCar.h"
#import "IssueVC.h"
#import "xianhuoVC.h"
#import "MySelfVC.h"

@interface RootTabBarViewController () <RDVTabBarControllerDelegate>

@property (nonatomic, strong) HearderVC *hearderVC;
@property (nonatomic, strong) ShopingCar *shopingCar;
@property (nonatomic, strong) IssueVC *issueVC;
@property (nonatomic, strong) xianhuoVC *xianhuoViewController;
@property (nonatomic, strong) MySelfVC *myselfViewController;

@end

@implementation RootTabBarViewController

+ (RootTabBarViewController *)rootViewController {
    RootTabBarViewController *rootViewController = [[RootTabBarViewController alloc] init];
    
    HearderVC *hearderVC = [[HearderVC alloc] init];
    ShopingCar *shopingCar = [[ShopingCar alloc] init];
    IssueVC *issueVC = [[IssueVC alloc] init];
    xianhuoVC *xianhuoViewController = [[xianhuoVC alloc] init];
    MySelfVC *myselfViewController = [[MySelfVC alloc] init];
    
    UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:hearderVC];
    UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:shopingCar];
    UINavigationController *thirdNavigationController = [[UINavigationController alloc] initWithRootViewController:issueVC];
    UINavigationController *fourthNavigationController = [[UINavigationController alloc] initWithRootViewController:xianhuoViewController];
    UINavigationController *fifthNavigationController = [[UINavigationController alloc] initWithRootViewController:myselfViewController];
    
    [rootViewController setViewControllers:@[firstNavigationController, secondNavigationController, thirdNavigationController, fourthNavigationController, fifthNavigationController]];
    
    return rootViewController;
}


- (id)init {
    self = [super init];
    if (self) {
        self.delegate = self;
    }
    return self;
}

- (void)dealloc {
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadCustomTabBar];
}

#pragma mark - 自定义的tababr
- (void)loadCustomTabBar {
    //定义分栏的 选中 不选中 字体颜色
    UIColor *tabbarColor = [[UIColor whiteColor] colorWithAlphaComponent:0.96];
    UIColor *titleNormalColor = RGBCOLOR(99, 103, 104);
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleNormalColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    
    UIColor *titleHighlightedColor = RGBCOLOR(73, 192, 219);
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];
    for (RDVTabBarItem* item in [[self tabBar] items]) {
        item.selectedTitleAttributes = @{NSFontAttributeName:Font(11),NSForegroundColorAttributeName:titleHighlightedColor};
        item.unselectedTitleAttributes = @{NSFontAttributeName:Font(11),NSForegroundColorAttributeName:titleNormalColor};
        item.badgeBackgroundColor = tabbarColor;
    }
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 1)];
    lineView.backgroundColor = RGBCOLOR(230, 232, 241);
    [self.tabBar addSubview:lineView];
    
    //首页
    RDVTabBarItem *headerTabBarItem = [[self tabBar] items][0];
    headerTabBarItem.title = @"首页";
    UIImage *headerVCSelectImage = [[UIImage imageNamed:@"home_hr"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [headerTabBarItem setFinishedSelectedImage:headerVCSelectImage withFinishedUnselectedImage:[UIImage imageNamed:@"home"]];
    
    //求购页
    RDVTabBarItem *shoppingCarTabBarItem = [[self tabBar] items][1];
    shoppingCarTabBarItem.title = @"求购";
    UIImage *shoppingVCSelectImage = [[UIImage imageNamed:@"buy_hr"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [shoppingCarTabBarItem setFinishedSelectedImage:shoppingVCSelectImage withFinishedUnselectedImage:[UIImage imageNamed:@"buy"]];
    
    UIImageView*coreImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_top_half"]];
    coreImage.frame=CGRectMake(SCREEN_WIDTH/2-20, -11, 40, 11.5);
    [self.tabBar addSubview:coreImage];
    
    //发布求购页
//    RDVTabBarItem *issueTabBarItem = [[self tabBar] items][2];
//    issueTabBarItem.title = @"";
//    UIImage *issueVCSelectImage = [[UIImage imageNamed:@"Release_for_hl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [issueTabBarItem setFinishedSelectedImage:issueVCSelectImage withFinishedUnselectedImage:[UIImage imageNamed:@"Release_for"]];
    
    UIImageView *addImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Release_for"]];
    addImage.alpha=1;
    addImage.frame=CGRectMake(SCREEN_WIDTH/2-20, -6, 40, 40);
    [self.tabBar addSubview:addImage];
    
    //信用行现货
    RDVTabBarItem *xianhuoTabBarItem = [[self tabBar] items][3];
    xianhuoTabBarItem.title = @"商城";
    UIImage *xianhuoVCSelectImage = [[UIImage imageNamed:@"Spot-goods_hr"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [xianhuoTabBarItem setFinishedSelectedImage:xianhuoVCSelectImage withFinishedUnselectedImage:[UIImage imageNamed:@"Spot-goods"]];
    
    //我的个人页面
    RDVTabBarItem *myselfTabBarItem = [[self tabBar] items][4];
    myselfTabBarItem.title = @"我的";
    UIImage *myselfVCSelectImage = [[UIImage imageNamed:@"Center_hr"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [myselfTabBarItem setFinishedSelectedImage:myselfVCSelectImage withFinishedUnselectedImage:[UIImage imageNamed:@"Center"]];
}

- (void)changeRootViewControllerTo:(int)num {
    if (num > [self.tabBar items].count) return;
    self.selectedIndex = num;
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    window.rootViewController = self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
    return NO;
}

#pragma mark - RDVTabBarControllerDelegate
- (BOOL)tabBarController:(RDVTabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return YES;
}

- (void)tabBarController:(RDVTabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}

#pragma mark - lazy load
- (HearderVC *)hearderVC {
    if (_hearderVC == nil) {
        _hearderVC = [[HearderVC alloc] init];
    }
    return _hearderVC;
}

- (ShopingCar *)shopingCar {
    if (_shopingCar == nil) {
        _shopingCar = [[ShopingCar alloc] init];
    }
    return _shopingCar;
}

- (IssueVC *)issueVC {
    if (_issueVC == nil) {
        _issueVC = [[IssueVC alloc] init];
    }
    return _issueVC;
}

- (xianhuoVC *)xianhuoViewController {
    if (_xianhuoViewController == nil) {
        _xianhuoViewController = [[xianhuoVC alloc] init];
    }
    return _xianhuoViewController;
}

- (MySelfVC *)myselfViewController {
    if (_myselfViewController == nil) {
        _myselfViewController = [[MySelfVC alloc] init];
    }
    return _myselfViewController;
}

@end
